package top.xia17.plugins.fast.common.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import top.xia17.plugins.fast.common.exceptions.BadRequestException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 返回格式
 * @author  夏十七
 * @date  2019/7/21 14:10
 * @version 1.0
 */
@Data
@Slf4j
@NoArgsConstructor
@Accessors(chain = true)
public class Back<T> {

    public Back(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public Back(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    /** status */
    private int status;

    /** 消息 */
    private String msg;

    /** 数据 */
    private T data;

    /** 附加内容 */
    private Object extra;

    public static<T> Back<T> ok(){
        return new Back<>(HttpStatus.OK.value(),"OK");
    }

    public static<T> Back<T> ok(String msg){
        return new Back<>(HttpStatus.OK.value(),msg);
    }

    public static<T> Back<T> ok(String msg , T data){
        return new Back<>(HttpStatus.OK.value(),msg,data);
    }

    public static<T> Back<T> ok(T data){
        return new Back<>(HttpStatus.OK.value(),"OK",data);
    }

    public static Back<String> okStr(String data){
        return new Back<>(HttpStatus.OK.value(),"OK",data);
    }

    public static<T> Back<PageData<T>> page(List<T> data, int count){
        return ok(new PageData<>(data, (long) count));
    }

    public static<T> Back<PageData<T>> page(List<T> data, long count){
        return ok(new PageData<>(data, count));
    }

    /**
     * 假装分页
     * @param list /
     * @return /
     */
    public static<T> Back<PageData<T>> page(List<T> list, int page , int size){
        if (page < 1) {
            page = 1;
        }
        if (size < 1){
            size = 100;
        }
        page = page - 1;
        ArrayList<T> result = new ArrayList<>();
        for (int i = page * size , j=0 ; i<list.size() && j<size ; i++ , j++){
            result.add(list.get(i));
        }
        return page(result,list.size());
    }

    public static<T> Back<T> error(){
        return new Back<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"系统未知异常，请联系管理员");
    }

    public static<T> Back<T> error(String msg){
        return new Back<>(HttpStatus.INTERNAL_SERVER_ERROR.value() , msg);
    }

    public static<T> Back<T> error(int status, String msg){
        return new Back<>(status,msg);
    }

    public static<T> Back<T> error(int status, String msg, T data){
        return new Back<>(status,msg,data);
    }




    /**
     * 下载文件
     * @param fileName 文件名
     * @param outputStream 输出流
     * @return /
     */
    public static ResponseEntity<Resource> file(String fileName, ByteArrayOutputStream outputStream) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("charset", "utf-8");
        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        headers.add("Content-Disposition", "attachment;fileName=\"" + fileName + "\"");
        Resource resource = new InputStreamResource(new ByteArrayInputStream(outputStream.toByteArray()));
        return ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType("application/x-msdownload")).body(resource);
    }


    /**
     * 返回图片
     * @param outputStream /
     * @param imgType /
     * @return /
     */
    public static ResponseEntity<Resource> img(ByteArrayOutputStream outputStream, String imgType){
        Resource resource = new InputStreamResource(new ByteArrayInputStream(outputStream.toByteArray()));
        return img(resource,imgType);
    }

    /**
     * 返回图片
     * @param resource /
     * @param imgType /
     * @return /
     */
    public static ResponseEntity<Resource> img(Resource resource, String imgType){
        if (imgType == null || "".equals(imgType)){
            log.error("图片格式为空");
            throw new BadRequestException("图片格式为空");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("charset", "utf-8");
        return ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType("image/" + imgType)).body(resource);
    }





}
